package com.inossem.warehousestorage_inossem.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.utils.base.ArithUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInPositionsActivity;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationGetSNsByPackageBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInPositionBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInSubmitRequestBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutOverViewBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutStorageBaseInfoBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutSubmitRequestBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.PositionListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNItemInfo;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 調撥
 *
 * @author 詹建宇
 * @time on 2018/3/29 15:16
 * @email jianyu.zhan@inossem.com
 */
public class AllocationUtils {

    /**
     * 选择生产入库单
     */
    public static List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> filterInputOrderList(String mCondition, List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> list) {
        List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> mlist = new ArrayList<>();
        for (AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean bean : list) {
            if (bean.getStock_input_code().contains(mCondition)) mlist.add(bean);
        }
        return mlist;
    }

    /**
     * 过滤后添加进来的物料
     */
    public static List<ItemListBean> filterItemList(List<ItemListBean> allData, List<ItemListBean> beanList) {

        for (int i = beanList.size() - 1; i >= 0; i--) {
            for (int j = 0; j < allData.size(); j++) {
                if (beanList.get(i).getMat_id().equals(allData.get(j).getMat_id())) {
                    beanList.remove(i);
                    break;
                }
            }
        }

        allData.addAll(beanList);
        return allData;
    }

    public static AllocationOutSubmitRequestBean getAllocationOutSubmitRequestBean(Context context, String stock_output_id, String mark) {

        String pickingOutStorageInformation = SPUtils.getAllotionOutStorageInformation(context);
        Gson gson = new Gson();
        AllocationOutOverViewBean allocationOutOverViewBean = gson.fromJson(pickingOutStorageInformation, AllocationOutOverViewBean.class);

        AllocationOutSubmitRequestBean submitBean = new AllocationOutSubmitRequestBean();
        submitBean.setRefer_receipt_id(allocationOutOverViewBean.getBody().getRefer_receipt_id());
        submitBean.setRefer_receipt_code(allocationOutOverViewBean.getBody().getRefer_receipt_code());
        submitBean.setRemark(mark);
        submitBean.setStock_output_id(stock_output_id);

        List<ItemListBean> resultItemList = new ArrayList<>();
        List<ItemListBean> itemList = allocationOutOverViewBean.getBody().getItem_list();
        for (int i = 0; i < itemList.size(); i++) {
            ItemListBean item = itemList.get(i);
            if (!TextUtils.isEmpty(item.getActualSendNumber())) {
                resultItemList.add(createItem(item, (i + 1) + ""));
            }
        }
        submitBean.setItem_list(resultItemList);

        return submitBean;
    }

    private static ItemListBean createItem(ItemListBean item, String stockOutputRid) {
        ItemListBean resultItem = new ItemListBean();
        resultItem.setStock_output_rid(stockOutputRid);
        resultItem.setRefer_receipt_rid(item.getRefer_receipt_rid());
        resultItem.setMat_id(item.getMat_id());
        resultItem.setUnit_id(item.getUnit_id());
        resultItem.setDecimal_place(item.getDecimal_place());
        resultItem.setFty_id(item.getFty_id());
        resultItem.setLocation_id(item.getLocation_id());
        resultItem.setOrder_qty(item.getOrder_qty());
        resultItem.setSend_qty(item.getSend_qty());
        resultItem.setOutput_qty(item.getActualSendNumber());//实发数量
        resultItem.setOrder_qty(item.getActualSendNumber());
        List<PositionListBean> positionList = new ArrayList<>();
        List<BatchBean.BatchInfoBean> itemBatchList = item.getBatchList();
        List<SNBean.SNInfoBean> itemSNList = item.getSnList();
        if (itemBatchList != null && !itemBatchList.isEmpty()) {
            for (int j = 0; j < itemBatchList.size(); j++) {
                PositionListBean positionItem = new PositionListBean();
                BatchBean.BatchInfoBean itemBatchInfo = itemBatchList.get(j);
                positionItem.setStock_output_position_id((j + 1) + "");
                positionItem.setStock_id(itemBatchInfo.getStock_id());
                positionItem.setQty(itemBatchInfo.getQty());
                positionItem.setSn_list(new ArrayList<>());
                positionList.add(positionItem);
            }
        }
        int tempNumber = (positionList == null || positionList.isEmpty()) ? 1 : positionList.size();
        if (itemSNList != null && !itemSNList.isEmpty()) {
            for (int j = 0; j < itemSNList.size(); j++) {
                PositionListBean positionItem = new PositionListBean();
                SNBean.SNInfoBean itemSNInfo = itemSNList.get(j);
                positionItem.setStock_output_position_id(tempNumber + j + "");
                positionItem.setStock_id(itemSNInfo.getStock_id());
                positionItem.setSn_list(itemSNInfo.getSn_list());
                positionItem.setQty(String.valueOf(itemSNInfo.getSn_list().size()));
                positionList.add(positionItem);
            }
        }
        resultItem.setPosition_list(positionList);
        return resultItem;
    }

//    public static String submitoutStorageSN(Context context, String refer_receipt_id, String refer_receipt_code, String mark) {
//        String pickingOutStorageInformation = SPUtils.getAllotionOutStorageInformation(context);
//        Gson gson = new Gson();
//        AllocationOutOverViewBean allocationOutOverViewBean = gson.fromJson(pickingOutStorageInformation, AllocationOutOverViewBean.class);
//
//        AllocationOutSubmitRequestBean submitBean = new AllocationOutSubmitRequestBean();
//        submitBean.setRefer_receipt_id(refer_receipt_id);
//        submitBean.setRefer_receipt_code(refer_receipt_code);
//        submitBean.setRemark(mark);
//
//        //物料层
//        List<AllocationOutSubmitRequestBean.ItemListBean> submitItemListBeans = new ArrayList<>();
//
//        List<ItemListBean> itemListBeans = allocationOutOverViewBean.getBody().getItem_list();
//        for (int i = 0; i < itemListBeans.size(); i++) {
//            ItemListBean itemListBean = itemListBeans.get(i);
//            AllocationOutSubmitRequestBean.ItemListBean submitItemListBean = new AllocationOutSubmitRequestBean.ItemListBean();
//
//            submitItemListBean.setStock_output_rid(String.valueOf(i));
//            submitItemListBean.setRefer_receipt_rid(itemListBean.getRefer_receipt_rid());
//            submitItemListBean.setMat_id(itemListBean.getMat_id());
//            submitItemListBean.setUnit_id(itemListBean.getUnit_id());
//            submitItemListBean.setDecimal_place(itemListBean.getDecimal_place());
//            submitItemListBean.setFty_id(itemListBean.getFty_id());
//            submitItemListBean.setLocation_id(itemListBean.getLocation_id());
//            submitItemListBean.setOrder_qty(itemListBean.getActualSendNumber());
//            submitItemListBean.setOutput_qty(itemListBean.getActualSendNumber());
//
//
//            List<SNBean.SNInfoBean> snInfoBeans = itemListBean.getSnList();
//
//            //仓位集合
//            List<SNItemInfo> snItemInfos = OutStorageUtils.changeSNFormat(snInfoBeans);
//            for (int j = snItemInfos.size(); j >= 0; j--) {
//                if (!snItemInfos.get(j).getType().equals(OutStorageUtils.SNTYPE_POSITION)) {
//                    snItemInfos.remove(j);
//                }
//            }
//
//            //上传的仓位集合
//            List<AllocationOutSubmitRequestBean.ItemListBean.PositionListBean> submitPositionListBeans = new ArrayList<>();
//            for (int j = 0; j < snItemInfos.size(); j++) {
//                AllocationOutSubmitRequestBean.ItemListBean.PositionListBean positionListBean = new AllocationOutSubmitRequestBean.ItemListBean.PositionListBean();
//                positionListBean.setStock_output_position_id(String.valueOf(j));
//
//                for (SNItemInfo snItemInfo : snItemInfos) {
//
//                }
//
//
//                submitPositionListBeans.add(positionListBean);
//            }
//
//
////            for (SNItemInfo snItemInfo : snItemInfos) {
////                String position_code = snItemInfo.getCode();
////
////                for (int j = 0; j < snInfoBeans.size(); j++) {
////                    SNBean.SNInfoBean snInfoBean = snInfoBeans.get(j);
////                    if (snInfoBean.getPosition_code().equals(position_code)) {
////                        AllocationOutSubmitRequestBean.ItemListBean.PositionListBean submitPpositionListBean = new AllocationOutSubmitRequestBean.ItemListBean.PositionListBean();
////                        submitPpositionListBean.setStock_output_position_id(String.valueOf(j));
////                        submitPpositionListBean.setQty(null);
////
////                        List<SNBean.SNInfoBean.SnListBean> snListBeans = snInfoBean.getSn_list();
////
////                    }
////                }
////            }
//
//
//        }
//
//    }

    //******************调拨入库**********************

    /**
     * 通过物料Id查到物料
     */
    public static AllocationInMatsBean.MatsBodyBean.ItemListBean getItemListBeanByMatId(Context context, String mat_id) {
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = new AllocationInMatsBean.MatsBodyBean.ItemListBean();
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : list) {
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                break;
            }
        }
        return bean;
    }

    /**
     * 未指定仓位的 仓位CODE
     */
    public static final String NO_CODE_POSITION = "未指定仓位";

    /**
     * 加 未指定仓位
     */
    public static List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> addNoCodePosition(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list) {
        boolean isHaveNoCodePlace = false;
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean stockPositionListBean : list) {
            if (String.valueOf(stockPositionListBean.getPosition_code()).equals(AllocationUtils.NO_CODE_POSITION)) {
                isHaveNoCodePlace = true;
                break;
            }
        }

        if (!isHaveNoCodePlace) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean positionListBean = new AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean();
            positionListBean.setPosition_code(AllocationUtils.NO_CODE_POSITION);
            positionListBean.setBatchOrSnIsOk("1");
            list.add(0, positionListBean);
        }
        return list;
    }

    /**
     * 获取确认了多少序列号
     */
    public static int getInNumFromListByBatchOrSnIsOk(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list) {
        int inNum = 0;
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (!TextUtils.isEmpty(bean.getBatchOrSnIsOk()) && bean.getBatchOrSnIsOk().equals("1")) {
                inNum++;
            }
        }
        return inNum;
    }

    /**
     * 获取默认仓位下序列号数量
     */
    public static int getInNumFromDefaultListByMatId(Context context, String mat_id) {
        int inNum = 0;
        AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = getItemListBeanByMatId(context, mat_id);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> snList = itemListBean.getSn_List();
        for (int i = 0; i < snList.size(); i++) {
            inNum++;
        }
        return inNum;
    }

    /**
     * 获取需要显示多少仓位 去重
     */
    public static List<AllocationInPositionBean> getShowList(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list) {

        List<AllocationInPositionBean> showList = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (!TextUtils.isEmpty(bean.getPosition_code()) && String.valueOf(bean.getBatchOrSnIsOk()).equals("1")) {
                //去重
                boolean isHaveThisPosition = false;
                for (AllocationInPositionBean allocationInPositionBean : showList) {
                    if (bean.getPosition_code().equals(allocationInPositionBean.getPosition_code())) {
                        isHaveThisPosition = true;
                        break;
                    }
                }
                if (!isHaveThisPosition) {
                    AllocationInPositionBean allocationInPositionBean = new AllocationInPositionBean();
                    allocationInPositionBean.setPosition_code(bean.getPosition_code());
                    allocationInPositionBean.setPosition_id(bean.getPosition_id());
                    allocationInPositionBean.setArea_id(bean.getArea_id());
                    allocationInPositionBean.setWh_id(bean.getWh_id());
                    showList.add(allocationInPositionBean);
                }
            }

        }
        return showList;
    }

    /**
     * 获取该物料下 对应仓位下的 已确认的序列号列表
     */
    public static List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> getSNList(Context context, String mat_id, String position_code) {
        AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = getItemListBeanByMatId(context, mat_id);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list = itemListBean.getStock_position_list();
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> showList = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (String.valueOf(position_code).equals(String.valueOf(bean.getPosition_code())) && String.valueOf(bean.getBatchOrSnIsOk()).equals("1")) {
                showList.add(bean);
            }
        }
        return showList;
    }

    /**
     * 获取序列号列表中的 不重复的 打包码集合
     */
    public static List<String> getPackageCodes(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list) {
        List<String> allCodes = new ArrayList<>();
        //获取所有打包码
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (!TextUtils.isEmpty(bean.getPackage_code())) {
                allCodes.add(bean.getPackage_code());
            }
        }
        //去重
        List<String> codes = new ArrayList<>();
        for (String code1 : allCodes) {
            boolean isHaveThisCode = false;
            for (String code2 : codes) {
                if (code1.equals(code2)) {
                    isHaveThisCode = true;
                    break;
                }
            }
            if (!isHaveThisCode) {
                codes.add(code1);
            }
        }

        return codes;
    }

    /**
     * 获取序列号列表中的 不属于任何打包码下的 序列号集合
     */
    public static List<String> getSNListNoPackage(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list) {
        List<String> snList = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (TextUtils.isEmpty(bean.getPackage_code())) {
                snList.add(bean.getSn_code());
            }
        }
        return snList;
    }

    /**
     * 获取该打包码下的序列号
     */
    public static List<SNItemInfo> getSNsByPackage(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list, String package_code) {
        List<SNItemInfo> snItemInfos = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : list) {
            if (!TextUtils.isEmpty(bean.getPackage_code()) && package_code.equals(bean.getPackage_code())) {
                SNItemInfo snItemInfo = new SNItemInfo("序列号", bean.getSn_code());
                snItemInfos.add(snItemInfo);
            }
        }
        return snItemInfos;
    }

    /**
     * 获取默认仓位下的 序列号列表中的 不重复的 打包码集合
     */
    public static List<String> getDefaultPackageCodes(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> allSNList) {
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> okList = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.SN sn : allSNList) {
            if (!TextUtils.isEmpty(sn.getPackage_code())) {
                okList.add(sn);
            }
        }

        List<String> codes = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.SN sn : okList) {
            boolean isHaveThisCode = false;
            for (String code : codes) {
                if (sn.getPackage_code().equals(code)) {
                    isHaveThisCode = true;
                    break;
                }
            }
            if (!isHaveThisCode) {
                codes.add(sn.getPackage_code());
            }
        }
        return codes;
    }

    /**
     * 默认仓位下的 序列号列表中的 不在打包码中的 序列号集合
     */
    public static List<String> getDefaultSNList(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> allSNList) {
        List<String> snList = new ArrayList<>();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.SN sn : allSNList) {
            if (TextUtils.isEmpty(sn.getPackage_code())) {
                snList.add(sn.getSn_code());
            }
        }
        return snList;
    }

    /**
     * 删除默认仓位下序列号
     */
    public static void deleteDefaultSN(Context context, String mat_id, String snCode) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : list) {
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                break;
            }
        }
        if (bean != null) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> snList = bean.getSn_List();
            for (int i = 0; i < snList.size(); i++) {
                if (snList.get(i).getSn_code().equals(snCode)) {
                    snList.remove(i);
                    break;
                }
            }
            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 删除默认仓位下打包码
     */
    public static void deleteDefaultPackage(Context context, String mat_id, String packageCode) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : list) {
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                break;
            }
        }
        if (bean != null) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> snList = bean.getSn_List();
            for (int i = snList.size() - 1; i >= 0; i--) {
                if (snList.get(i).getPackage_code().equals(packageCode)) {
                    snList.remove(i);
                }
            }
            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 删除仓位下的序列号
     */
    public static void deleteSN(Context context, String mat_id, String snCode) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : list) {
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                break;
            }
        }
        if (bean != null) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> snList = bean.getStock_position_list();
            for (int i = 0; i < snList.size(); i++) {
                if (snList.get(i).getSn_code().equals(snCode)) {
                    snList.get(i).setBatchOrSnIsOk(null);
                    snList.get(i).setPosition_code(null);
                    break;
                }
            }
            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 删除仓位下打包码
     */
    public static void deletePackage(Context context, String mat_id, String packageCode) {
        String json = SPUtils.getAllocationInInformation(context);

        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        int z = 999;
        for (int i = 0; i < list.size(); i++) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = list.get(i);
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                z = i;
                break;
            }
        }
        if (bean != null) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> snList = bean.getStock_position_list();
            for (int i = 0; i < snList.size(); i++) {
                if (snList.get(i).getPackage_code().equals(packageCode)) {
                    snList.get(i).setBatchOrSnIsOk(null);
                    snList.get(i).setPosition_code(null);
                    snList.get(i).setPosition_id(null);
                    snList.get(i).setArea_id(null);
                    snList.get(i).setWh_id(null);
                }
            }

            allocationInMatsBean.getBody().getItem_list().get(z).setStock_position_list(snList);

            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 将扫出来的序列号 放到默认仓位下
     */
    public static void setSNsInDefaultPosition(Context context, String mat_id, AllocationGetSNsByPackageBean b) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        int z = 999;
        for (int i = 0; i < list.size(); i++) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = list.get(i);
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                z = i;
                break;
            }
        }
        if (bean != null && z != 999) {
            List<AllocationGetSNsByPackageBean.SNBodyBean.SnListBean> searchSNList = b.getBody().getSn_list();
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> defaultSnList = bean.getSn_List();
            for (AllocationGetSNsByPackageBean.SNBodyBean.SnListBean b1 : searchSNList) {
                boolean isHave = false;
                for (AllocationInMatsBean.MatsBodyBean.ItemListBean.SN b2 : defaultSnList) {
                    if (b1.getSn_code().equals(b2.getSn_code())) {
                        isHave = true;
                        break;
                    }
                }
                if (!isHave) {
                    AllocationInMatsBean.MatsBodyBean.ItemListBean.SN sn = new AllocationInMatsBean.MatsBodyBean.ItemListBean.SN();
                    sn.setPackage_code(b.getBody().getPackage_code());
                    sn.setPackage_id(b.getBody().getPackage_id());
                    sn.setSn_code(b1.getSn_code());
                    sn.setSn_id(b1.getSn_id());
                    defaultSnList.add(sn);
                }
            }

            allocationInMatsBean.getBody().getItem_list().get(z).setSn_List(defaultSnList);

            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 将扫出来的序列号 放到该仓位下(没确定的序列号集合 跟 search出来的 进行匹配(序列号 打包码) 匹配OK的 填入该仓位信息)
     */
    public static void setSNsInPosition(Context context, String mat_id, AllocationGetSNsByPackageBean b,
                                        String position_code, String position_id, String area_id, String wh_id) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        int z = 999;
        for (int i = 0; i < list.size(); i++) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = list.get(i);
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                z = i;
                break;
            }
        }
        if (bean != null) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> snList = bean.getStock_position_list();
            List<AllocationGetSNsByPackageBean.SNBodyBean.SnListBean> searchSNList = b.getBody().getSn_list();

            for (int i = 0; i < snList.size(); i++) {
                boolean isOk = false;
                for (AllocationGetSNsByPackageBean.SNBodyBean.SnListBean b2 : searchSNList) {
                    if (snList.get(i).getSn_code().equals(b2.getSn_code())
                            && String.valueOf(snList.get(i).getPackage_code()).equals(String.valueOf(b.getBody().getPackage_code()))) {
                        isOk = true;
                        break;
                    }
                }
                if (isOk) {
                    snList.get(i).setBatchOrSnIsOk("1");
                    snList.get(i).setPosition_code(position_code);
                    snList.get(i).setPosition_id(position_id);
                    snList.get(i).setArea_id(area_id);
                    snList.get(i).setWh_id(wh_id);
                }
            }

            allocationInMatsBean.getBody().getItem_list().get(z).setStock_position_list(snList);

            json = gson.toJson(allocationInMatsBean);

            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 根据物料Id 批次Id 获取该批次的入库数量(批次)
     */
    public static double getInQtyByMatIdBatch(Context context, String mat_id, String batch) {
        AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = getItemListBeanByMatId(context, mat_id);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList = itemListBean.getStock_position_list();
        double num = 0;
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : stockPositionList) {
            if (batch.equals(bean.getBatch())) {
                List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> positionList = bean.getPositionList();
                if (positionList != null) {
                    for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position position : positionList) {
                        if (position.getNumber() != null) {
                            num = ArithUtils.add(num, Double.parseDouble(position.getNumber()));
                        }
                    }
                }
            }
        }
        return num;
    }

    /**
     * 根据物料Id 获取该物料的入库数量(批次)
     */
    public static double getInQtyByMatId(Context context, String mat_id) {
        AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = getItemListBeanByMatId(context, mat_id);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList = itemListBean.getStock_position_list();
        double num = 0.0;
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean positionListBean : stockPositionList) {
            num = ArithUtils.add(num, getInQtyByMatIdBatch(context, mat_id, positionListBean.getBatch()));
        }
        return num;
    }

    /**
     * 默认仓位下的序列号集合 放入扫出来的仓位下
     */
    public static void setDefaultSNsInPosition(Context context, String mat_id, AllocationGetSNsByPackageBean getSNsByPackageBean) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        int z = 999;
        for (int i = 0; i < list.size(); i++) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = list.get(i);
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                z = i;
                break;
            }
        }
        if (bean != null && z != 999) {

            //序列号集合
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList = bean.getStock_position_list();
            //默认仓位下 序列号集合
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> snList = bean.getSn_List();

            for (int i = 0; i < stockPositionList.size(); i++) {
                if (!String.valueOf(stockPositionList.get(i).getBatchOrSnIsOk()).equals("1") && TextUtils.isEmpty(stockPositionList.get(i).getPosition_code())) {
                    for (int j = snList.size() - 1; j >= 0; j--) {
                        if (stockPositionList.get(i).getSn_code().equals(snList.get(j).getSn_code()) && String.valueOf(stockPositionList.get(i).getPackage_code()).equals(String.valueOf(snList.get(j).getPackage_code()))) {
                            stockPositionList.get(i).setBatchOrSnIsOk("1");
                            stockPositionList.get(i).setPosition_id(getSNsByPackageBean.getBody().getPosition_id());
                            stockPositionList.get(i).setPosition_code(getSNsByPackageBean.getBody().getPosition_code());
                            stockPositionList.get(i).setWh_id(getSNsByPackageBean.getBody().getWh_id());
                            stockPositionList.get(i).setArea_id(getSNsByPackageBean.getBody().getArea_id());
                            snList.remove(j);
                        }
                    }
                }
            }

            allocationInMatsBean.getBody().getItem_list().get(z).setStock_position_list(stockPositionList);
            allocationInMatsBean.getBody().getItem_list().get(z).setSn_List(snList);

            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 根据物料Id 批次号 获取仓位列表(批次)
     */
    public static List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> getPositionsByMatIdBatch(Context context, String mat_id, String batch) {
        AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = getItemListBeanByMatId(context, mat_id);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> list = new ArrayList<>();
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> positionList = itemListBean.getStock_position_list();
        for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean : positionList) {
            if (bean.getBatch().equals(batch)) {
                list = bean.getPositionList();
                break;
            }
        }
        return list == null ? new ArrayList<>() : list;
    }

    /**
     * 动态添加仓位View
     */
    public static void createEditText(Context context, ViewGroup viewGroup, String positionCode, String text, String unitText, int id, AllocationInPositionsActivity.PositionsViewListener listener) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.cy_view_in_storage_edittext, null);
        view.setId(id);
        TextView position = (TextView) view.findViewById(R.id.position);
        TextView unit = (TextView) view.findViewById(R.id.unit);
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                DialogUtils.getConfirmDialog(context, "是否删除？", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        listener.deleteClickListener(view.getId(), position.getText().toString());
                    }
                }).show();

                return false;
            }
        });
        unit.setText(unitText);
        EditText textEditText = (EditText) view.findViewById(R.id.text);
        position.setText(positionCode);
        textEditText.setText(text);
        viewGroup.addView(view);
    }

    /**
     * 判断新添加的批次是否已经存在(批次)
     */
    public static boolean havePosition(List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> list, String position) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPosition_code().equals(position)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 保存仓位列表到物料 批次 下
     */
    public static void savePositionListInMat(Context context, String mat_id, String batch, List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> positionList) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        AllocationInMatsBean.MatsBodyBean.ItemListBean bean = null;
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        int z = 999;
        for (int i = 0; i < list.size(); i++) {
            AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean = list.get(i);
            if (itemListBean.getMat_id().equals(mat_id)) {
                bean = itemListBean;
                z = i;
                break;
            }
        }

        if (bean != null && z != 999) {
            List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList = bean.getStock_position_list();
            for (int i = 0; i < stockPositionList.size(); i++) {
                if (stockPositionList.get(i).getBatch().equals(batch)) {
                    stockPositionList.get(i).setPositionList(positionList);
                    break;
                }
            }

            allocationInMatsBean.getBody().getItem_list().get(z).setStock_position_list(stockPositionList);

            json = gson.toJson(allocationInMatsBean);
            SPUtils.saveAllocationInInformation(context, json);
        }
    }

    /**
     * 保存该物料状态 是否完成 SN
     */
    public static void saveIsOkThisMatOfSN(Context context, String mat_id, int inNum, int outNum) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMat_id().equals(mat_id)) {
                list.get(i).setIsOk(inNum == outNum ? "1" : "");
                list.get(i).setInNum(String.valueOf(inNum));
                list.get(i).setOutNum(String.valueOf(outNum));
                break;
            }
        }
        allocationInMatsBean.getBody().setItem_list(list);

        json = gson.toJson(allocationInMatsBean);
        SPUtils.saveAllocationInInformation(context, json);
    }

    /**
     * 保存该物料状态 是否完成 Batch
     */
    public static void saveIsOkThisMatOfBatch(Context context, String mat_id, String inNum, String outNum) {
        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);
        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list = allocationInMatsBean.getBody().getItem_list();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMat_id().equals(mat_id)) {
                list.get(i).setIsOk(inNum.equals(outNum) ? "1" : "");
                list.get(i).setInNum(inNum);
                list.get(i).setOutNum(outNum);
                break;
            }
        }
        allocationInMatsBean.getBody().setItem_list(list);

        json = gson.toJson(allocationInMatsBean);
        SPUtils.saveAllocationInInformation(context, json);
    }

    /**
     * 拼接提交Json SN
     */
    public static String submitInStorageSN(Context context, String allocate_id, String stock_input_id, String stock_input_code) {

        AllocationInSubmitRequestBean submitBean = new AllocationInSubmitRequestBean();

        submitBean.setAllocate_id(allocate_id);
        submitBean.setStock_input_id(stock_input_id);
        submitBean.setStock_input_code(stock_input_code);

        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);

        //物料层
        List<AllocationInSubmitRequestBean.ItemListBean> submitListBeans = new ArrayList<>();

        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> listBeanList = allocationInMatsBean.getBody().getItem_list();

        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : listBeanList) {

            AllocationInSubmitRequestBean.ItemListBean submitItemBean = new AllocationInSubmitRequestBean.ItemListBean();

            submitItemBean.setAllocate_item_id(itemListBean.getAllocate_item_id());

            //序列号层
            List<AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean> submitStockPositionList = new ArrayList<>();

            //添加正常仓位
            for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean positionList : itemListBean.getStock_position_list()) {

                //单个序列号
                AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean submitStockPositionBean = new AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean();

                submitStockPositionBean.setArea_id(positionList.getArea_id());
                submitStockPositionBean.setBatch(positionList.getBatch());
                submitStockPositionBean.setPackage_id(positionList.getPackage_id());
                submitStockPositionBean.setSn_id(positionList.getSn_id());
                submitStockPositionBean.setWh_id(positionList.getWh_id());
                submitStockPositionBean.setPosition_id(positionList.getPosition_id());
                submitStockPositionBean.setQty("1");

                submitStockPositionList.add(submitStockPositionBean);
            }

            //添加默认仓位
            for (AllocationInMatsBean.MatsBodyBean.ItemListBean.SN sn : itemListBean.getSn_List()) {
                //单个序列号
                AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean submitStockPositionBean = new AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean();

                submitStockPositionBean.setPackage_id(sn.getPackage_id());
                submitStockPositionBean.setSn_id(sn.getSn_id());
                submitStockPositionBean.setQty("1");

                submitStockPositionList.add(submitStockPositionBean);
            }

            submitItemBean.setStock_position_list(submitStockPositionList);

            submitListBeans.add(submitItemBean);
        }

        submitBean.setItem_list(submitListBeans);

        String j = gson.toJson(submitBean);

        Log.i("aaaaa序列号", j);
        return j;
    }

    /**
     * 拼接提交Json Batch
     */
    public static String submitInStorageBatch(Context context, String allocate_id, String stock_input_id, String stock_input_code) {

        AllocationInSubmitRequestBean submitBean = new AllocationInSubmitRequestBean();

        submitBean.setAllocate_id(allocate_id);
        submitBean.setStock_input_id(stock_input_id);
        submitBean.setStock_input_code(stock_input_code);

        String json = SPUtils.getAllocationInInformation(context);
        Gson gson = new Gson();
        AllocationInMatsBean allocationInMatsBean = gson.fromJson(json, AllocationInMatsBean.class);

        //物料层
        List<AllocationInSubmitRequestBean.ItemListBean> submitListBeans = new ArrayList<>();

        List<AllocationInMatsBean.MatsBodyBean.ItemListBean> listBeanList = allocationInMatsBean.getBody().getItem_list();

        for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : listBeanList) {

            //单个物料
            AllocationInSubmitRequestBean.ItemListBean submitItemListBean = new AllocationInSubmitRequestBean.ItemListBean();

            submitItemListBean.setAllocate_item_id(itemListBean.getAllocate_item_id());

            List<AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean> submitStockPositionListBeans = new ArrayList<>();

            for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean stockPosition : itemListBean.getStock_position_list()) {

                //批次
                String batch = stockPosition.getBatch();

                for (AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position position : stockPosition.getPositionList()) {

                    //单个批次
                    AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean submitPosition = new AllocationInSubmitRequestBean.ItemListBean.StockPositionListBean();
                    submitPosition.setPosition_id(position.getPosition_id());
                    submitPosition.setArea_id(position.getArea_id());
                    submitPosition.setWh_id(position.getWh_id());
                    submitPosition.setBatch(batch);
                    submitPosition.setQty(position.getNumber());

                    submitStockPositionListBeans.add(submitPosition);

                }
            }

            submitItemListBean.setStock_position_list(submitStockPositionListBeans);

            submitListBeans.add(submitItemListBean);
        }

        submitBean.setItem_list(submitListBeans);

        String j = gson.toJson(submitBean);

        Log.i("aaaaa批次", j);
        return j;
    }

//    public static boolean isOkTh
}
